app.service(
		"allocationService",
		function($http, ajaxRequest) {
			return {
				getListData : function(obj) {
					return ajaxRequest.request($http.get("/allocation/getListData",
							{
								params : obj
							}));
				},
				getDetaillistData : function(obj) {
					return ajaxRequest.request($http.get("/allocation/detaillist",
							{
								params : obj
							}));
				},
				getOneGoodsData : function(obj) {
					return ajaxRequest.request($http.get("/selectgoods/getOneGoodsData",
							{
								params : obj
							}));
				},
				saveData : function(obj) {
					return ajaxRequest.request($http.post("/allocation/save",obj));
				},
				setOrderState : function(obj) {
					return ajaxRequest.request($http.get("/allocation/setOrderState",
							{
								params : obj
							}));
				}
			}
		}).controller(
		"allocationIndexController",
		function($scope, allocationService) {
			$scope.orderState = 0;
			$scope.searchData = {
				orderCode : $scope.orderCode,
				orderState : $scope.orderState,
				outWarehouseId : $scope.outWarehouseId,
				inWarehouseId : $scope.inWarehouseId,
				outUserName : $scope.outUserName,
				startCreateTime : $scope.startCreateTime,
				endCreateTime : $scope.endCreateTime,
				startOutTime : $scope.startOutTime,
				endOutTime : $scope.endOutTime,
				startInTime : $scope.startInTime,
				endInTime : $scope.endInTime,
				pageIndex : 1,
				pageNum : 10
			};

			var requestListData = function() {
				$scope.searchData.orderCode = $scope.orderCode;
				$scope.searchData.orderState = $scope.orderState;
				$scope.searchData.outWarehouseId = $scope.outWarehouseId;
				$scope.searchData.inWarehouseId = $scope.inWarehouseId;
				$scope.searchData.outUserName = $scope.outUserName;
				$scope.searchData.startCreateTime = $scope.startCreateTime;
				$scope.searchData.endCreateTime = $scope.endCreateTime;
				$scope.searchData.startOutTime = $scope.startOutTime;
				$scope.searchData.endOutTime = $scope.endOutTime;
				$scope.searchData.startInTime = $scope.startInTime;
				$scope.searchData.endInTime = $scope.endInTime;
				allocationService.getListData($scope.searchData).then(
						function(data) {
							var re = data.result;
							$scope.AllocationOrderBoList = re.entityList;
							$scope.CurrentPage = re.curPageIndex;
							$scope.RecordCount = re.recordCount;
							$scope.ShowPages = re.pageCountList;
							$scope.PageCount = re.pageCount;
						}, function(data) {
							Kalert(data.msg);
						});
			}
            
			requestListData();

			$scope.GotoPage = function(pageindex) {
				if (pageindex <= 0)
					return;
				if (pageindex > $scope.PageCount)
					return;
				$scope.searchData.pageIndex = pageindex;
				requestListData($scope.searchData);
			};

			$scope.Search = function() {
				$scope.searchData.pageIndex = 1;
				requestListData();
			}

			$scope.ShowDetail = function(alo) {
				var orderCode=(alo.orderCode==null)?orderCode="":orderCode=alo.orderCode;
				var outWarehouseName= (alo.outWarehouseId==null)?outWarehouseName="":outWarehouseName=alo.outWarehouseName;
				var inWarehouseName = (alo.inWarehouseName==null)?inWarehouseName="":inWarehouseName=alo.inWarehouseName;
				var hopeOutTimeStr = (alo.hopeOutTime==null)?hopeOutTimeStr="":hopeOutTimeStr=alo.hopeOutTimeStr;
				var hopeInTimeStr = (alo.hopeInTime==null)?hopeInTimeStr="":hopeInTimeStr=alo.hopeInTimeStr;
				var allocationCost = (alo.allocationCost==null)?allocationCost="":allocationCost=alo.allocationCost;
				var remark = (alo.remark==null)?remark="":remark=alo.remark;
				
                window.location.href="/allocation/detail?orderId="+alo.id
                +"&orderCode="+orderCode
                +"&outWarehouseName="+outWarehouseName
                +"&inWarehouseName="+inWarehouseName
                +"&hopeOutTime="+hopeOutTimeStr
                +"&allocationCost="+allocationCost
                +"&hopeInTime="+hopeInTimeStr
                +"&remark="+encodeURIComponent(remark);
			}
			//编辑
			$scope.Edit = function(alo) {
				if(alo == null){
					window.location.href="/allocation/edit?orderId="+0
	                +"&orderCode="+""
	                +"&outWarehouseId="+""
	                +"&inWarehouseId="+""
	                +"&hopeOutTime="+""
	                +"&allocationCost="+""
	                +"&hopeInTime="+""
	                +"&remark="+"";
				}else{
					var orderCode=(alo.orderCode==null)?orderCode="":orderCode=alo.orderCode;
					var outWarehouseId= (alo.outWarehouseId==null)?outWarehouseId="":outWarehouseId=alo.outWarehouseId;
					var inWarehouseId = (alo.inWarehouseId==null)?inWarehouseId="":inWarehouseId=alo.inWarehouseId;
					var hopeOutTime = (alo.hopeOutTime==null)?hopeOutTime="":hopeOutTime=alo.hopeOutTimeStr;
					var hopeInTime = (alo.hopeInTime==null)?hopeInTime="":hopeInTime=alo.hopeInTimeStr;
					var allocationCost = (alo.allocationCost==null)?allocationCost="":allocationCost=alo.allocationCost;
					var remark = (alo.remark==null)?remark="":remark=alo.remark;
					
					window.location.href="/allocation/edit?orderId="+alo.id
	                +"&orderCode="+orderCode
	                +"&outWarehouseId="+outWarehouseId
	                +"&inWarehouseId="+inWarehouseId
	                +"&hopeOutTime="+hopeOutTime
	                +"&allocationCost="+allocationCost
	                +"&hopeInTime="+hopeInTime
	                +"&remark="+encodeURIComponent(remark);
				}
			}
			//审核
			$scope.Check = function(id){
				updateState(id,1);
			}
			//删除
			$scope.Cancel = function(id){
				updateState(id,6);
			}
			var updateState = function(id,state){
				$scope.checkData = {
						orderId : id,
						state : state
				}
				allocationService.setOrderState($scope.checkData).then(
					function(data) {
						if(state===1) Kalert("审核成功");
						if(state===6) Kalert("取消成功");
						requestListData();
					}, function(data) {
						Kalert(data.msg);
					});
			}
			//点击标签
			$scope.tabClick = function(id,state){
				$scope.orderState = state;
				RemoveCss();
				$("#" + id).addClass("active");
				$scope.searchData.pageIndex = 1;
				requestListData();
			}
			var RemoveCss = function() {
				$("#286433").removeClass("active");
				$("#286434").removeClass("active");
				$("#286435").removeClass("active");
			};
			//导出
			$scope.exportcsv = function(){
				var orderCode=(!$scope.orderCode)?orderCode="":orderCode=$scope.orderCode;
				var orderState= $scope.orderState;
				var outWarehouseId = (!$scope.outWarehouseId)?outWarehouseId="-1":outWarehouseId=$scope.outWarehouseId;
				var inWarehouseId = (!$scope.inWarehouseId)?inWarehouseId="-1":inWarehouseId=$scope.inWarehouseId;
				var outUserName = (!$scope.outUserName)?outUserName="":outUserName=$scope.outUserName;
				var startCreateTime = (!$scope.startCreateTime)?startCreateTime="":startCreateTime=$scope.startCreateTime;
				var endCreateTime = (!$scope.endCreateTime)?endCreateTime="":endCreateTime=$scope.endCreateTime;
				var startOutTime = (!$scope.startOutTime)?startOutTime="":startOutTime=$scope.startOutTime;
				var endOutTime = (!$scope.endOutTime)?endOutTime="":endOutTime=$scope.endOutTime;
				var startInTime = (!$scope.startInTime)?startInTime="":startInTime=$scope.startInTime;
				var endInTime = (!$scope.endInTime)?endInTime="":endInTime=$scope.endInTime;
				
				window.location.href = "/allocation/exportCsv?orderCode="+orderCode+
				"&orderState="+orderState+
				"&outWarehouseId="+outWarehouseId+
				"&inWarehouseId="+inWarehouseId+
				"&outUserName="+outUserName+
				"&startCreateTime="+startCreateTime+
				"&endCreateTime="+endCreateTime+
				"&startOutTime="+startOutTime+
				"&endOutTime="+endOutTime+
				"&startInTime="+startInTime+
				"&endInTime="+endInTime;
			};
			
			$scope.Reset = function(){
				 $scope.orderCode="";
				 $scope.outWarehouseId="";
				 $scope.inWarehouseId="";
				 $scope.outUserName="";
				 $scope.startCreateTime="";
				 $scope.endCreateTime="";
				 $scope.startOutTime="";
				 $scope.endOutTime="";
				 $scope.startInTime="";
				 $scope.endInTime="";
			}
			
		}).controller(
		"allocationDetailController",
		function($scope, allocationService) {

			var orderId = GetQueryString("orderId");
			$scope.searchData = {
					orderId : orderId
				};
			var requestWarehouse = function(){
            	outOrderNoticeService.getDetaillistData($scope.searchData).then(
						function(data) {
							$scope.DetaillistData = data;
						}, function(data) {
							Kalert(data.msg);
						});
            }
			requestWarehouse();
			$scope.back = function() {
				window.location.href = "/allocation/index";
			}
		}).controller(
			"allocationEditController",
			function($scope, allocationService) {
				var outWasrehouseIdTemp = null;
				var inWasrehouseIdTemp = null;
			var orderId = GetQueryString("orderId");
			var orderCode = GetQueryString("orderCode");
			var outWarehouseId = GetQueryString("outWarehouseId");
			var inWarehouseId = GetQueryString("inWarehouseId");
			var hopeOutTime = GetQueryString("hopeOutTime");
			var hopeInTime = GetQueryString("hopeInTime");
			var allocationCost = GetQueryString("allocationCost");
			var remark = GetQueryString("remark");
			outWasrehouseIdTemp = outWarehouseId;
			inWasrehouseIdTemp = inWarehouseId;
			var newallocationorderList = {
					id:"0",
				outGoodsId : "",
				outGoodsSku : "",
				outGoodsBarcode : "",
				outGoodsName : "",
				outGoodsStock : "",
				outGoodsUsestock : "",
				outGoodsPacking : "",
				inGoodsId : "",
				inGoodsSku : "",
				inGoodsBarcode : "",
				inGoodsName : "",
				
				planQuantity : ""
			};
			$scope.searchData = {
					orderId : orderId
			}
			var requestDetails = function (){
				$scope.allocationorderList = [];
				allocationService.getDetaillistData($scope.searchData).then(
					function(data) {
						for(var i=0;i<data.length;i++){
							$scope.allocationorderList.push({
								id:data[i].id,
								outGoodsId : data[i].outGoodsId,
								outGoodsSku : data[i].outGoodsSku,
								outGoodsBarcode : data[i].outGoodsBarcode,
								outGoodsName : data[i].outGoodsName,
								outGoodsStock : data[i].outGoodsStock,
								outGoodsUsestock : data[i].outGoodsUsestock,
								outGoodsPacking : data[i].outGoodsPacking,
								
								inGoodsId : data[i].inGoodsId,
								inGoodsSku : data[i].inGoodsSku,
								inGoodsBarcode : data[i].inGoodsBarcode,
								inGoodsName : data[i].inGoodsName,
								planQuantity : data[i].planQuantity
							});
						}
					}, function(data) {
						Kalert(data.msg);
					});
			}
			
			if (orderId == "0") {
				$scope.allocationorderList = [];
				$scope.allocationorderList.push(newallocationorderList);
			} else {
				requestDetails();
				$scope.outWarehouseId = outWarehouseId;
				$scope.inWarehouseId =inWarehouseId;
				$scope.allocationCost = allocationCost;
				$scope.hopeOutTime =hopeOutTime;
				$scope.hopeInTime =hopeInTime;
				$scope.remark =remark;
			}
			var addline = function() {
				$scope.allocationorderList.push({
					id:"0",

					outGoodsId : "",
					outGoodsSku : "",
					outGoodsBarcode : "",
					outGoodsName : "",
					outGoodsStock : "",
					outGoodsUsestock : "",
					outGoodsPacking : "",

					inGoodsId : "",
					inGoodsSku : "",
					inGoodsBarcode : "",
					inGoodsName : "",
					planQuantity : ""
				});
			}

			$scope.addd = function() {
				addline();
			}

			$scope.dell = function(rowindex) {
				if ($scope.allocationorderList.length > 1) {
					$scope.allocationorderList.splice(rowindex, 1);
				}
			}
			$scope.selectoutgoods = function(index) {
				if (!$scope.outWarehouseId) {
					Kalert("请先选择调出仓库");
					return false;
				}
				if (!$scope.inWarehouseId) {
					Kalert("请先选择调入仓库");
					return false;
				}
				if($scope.outWarehouseId ==$scope.inWarehouseId){
					Kalert("不能向与调出仓库相同的仓库调拨，请修改");
					return;
				}
				$.colorbox({
					href : "/selectgoods/index?warehouseId="+$scope.outWarehouseId,
					iframe : true,
					width : "800px",
					height : "540px",
					top : "50px",
					opacity : 0,
					overlayClose : false,
					scrolling : true,
					onClosed : function(o) {
						var goods = o.get("returnValue");
						if (goods != null) {
							if (IsExist(goods[0].goodsId)) {
								Kalert("此商品已存在");
							} else {
								$scope.allocationorderList[index].outGoodsId = goods[0].goodsId;
								$scope.allocationorderList[index].outGoodsSku = goods[0].sku;
								$scope.allocationorderList[index].outGoodsBarcode = goods[0].barcode;
								$scope.allocationorderList[index].outGoodsName = goods[0].goodsName;
								$scope.allocationorderList[index].outGoodsStock = goods[0].curStock;
								$scope.allocationorderList[index].outGoodsPacking = goods[0].packing;
								$scope.allocationorderList[index].outGoodsUsestock = goods[0].curUseStock;
								$scope.allocationorderList[index].planQuantity = "";
							}
						}
						$scope.$apply();
					}
				});
			}
			$scope.selectingoods = function(index) {
				if (!$scope.outWarehouseId) {
					Kalert("请先选择调出仓库");
					return false;
				}
				if (!$scope.inWarehouseId) {
					Kalert("请先选择调入仓库");
					return false;
				}
				if($scope.outWarehouseId ==$scope.inWarehouseId){
					Kalert("不能向与调出仓库相同的仓库调拨，请修改");
					return;
				}
				$.colorbox({
					href : "/selectgoods/index?warehouseId="+$scope.inWarehouseId,
					iframe : true,
					width : "800px",
					height : "540px",
					top : "50px",
					opacity : 0,
					overlayClose : false,
					scrolling : true,
					onClosed : function(o) {
						var goods = o.get("returnValue");
						if (goods != null) {
								$scope.allocationorderList[index].inGoodsId = goods[0].goodsId;
								$scope.allocationorderList[index].inGoodsSku = goods[0].sku;
								$scope.allocationorderList[index].inGoodsBarcode = goods[0].barcode;
								$scope.allocationorderList[index].inGoodsName = goods[0].goodsName;	
						}
						$scope.$apply();
					}
				});
			}
			//手动输入货号
			$scope.changeOutGoods = function(index) {
				if (!$scope.outWarehouseId) {
					$scope.allocationorderList[index].outGoodsSku = "";
					Kalert("请先选择调出仓库", "系统提示");
					return false;
				}
				if (!$scope.inWarehouseId) {
					$scope.allocationorderList[index].inGoodsSku = "";
					Kalert("请先选择调入仓库", "系统提示");
					return false;
				}
				if($scope.outWarehouseId ==$scope.inWarehouseId){
					Kalert("不能向与调出仓库相同的仓库调拨，请修改");
					return;
				}
				var selData = {
						sku : $scope.allocationorderList[index].outGoodsSku,
						warehouseId : $scope.outWarehouseId
				}
				if ($scope.allocationorderList[index].outGoodsSku != "") {
					allocationService.getOneGoodsData(selData).then(
							function(data) {
								var goods = data.result;
								if (IsExist(goods.goodsId)) {
									clearData(index);
									Kalert("此商品已存在");
								} else {
									$scope.allocationorderList[index].outGoodsId = goods.goodsId;
									$scope.allocationorderList[index].outGoodsSku = goods.sku;
									$scope.allocationorderList[index].outGoodsBarcode = goods.barcode;
									$scope.allocationorderList[index].outGoodsName = goods.goodsName;
									$scope.allocationorderList[index].outGoodsStock = goods.curStock;
									$scope.allocationorderList[index].outGoodsPacking = goods.packing;
									$scope.allocationorderList[index].outGoodsUsestock = goods.curUseStock;
									$scope.allocationorderList[index].planQuantity = "";
								}
							}, function(data) {
								clearData(index);
								Kalert(data.msg);
							});
				} else {
					clearData(index);
				}
			}
			$scope.changeInGoods = function(index) {
				if (!$scope.outWarehouseId) {
					$scope.allocationorderList[index].outGoodsSku = "";
					Kalert("请先选择调出仓库", "系统提示");
					return false;
				}
				if (!$scope.inWarehouseId) {
					$scope.allocationorderList[index].inGoodsSku = "";
					Kalert("请先选择调入仓库", "系统提示");
					return false;
				}
				if($scope.outWarehouseId ==$scope.inWarehouseId){
					Kalert("不能向与调出仓库相同的仓库调拨，请修改");
					return;
				}
				var selData = {
						sku : $scope.allocationorderList[index].inGoodsSku,
						warehouseId : $scope.inWarehouseId
				}
				if ($scope.allocationorderList[index].inGoodsSku != "") {
					allocationService.getOneGoodsData(selData).then(
							function(data) {
								var goods = data.result;
								$scope.allocationorderList[index].inGoodsId = goods.goodsId;
								$scope.allocationorderList[index].inGoodsSku = goods.sku;
								$scope.allocationorderList[index].inGoodsBarcode = goods.barcode;
								$scope.allocationorderList[index].inGoodsName = goods.goodsName;
							}, function(data) {
								clearDataIn(index);
								Kalert(data.msg);
							});
				} else {
					clearDataIn(index);
				}
			}
			$scope.saveOrder = function(){
				var submitData = {
						id:orderId,
						outWarehouseId:$scope.outWarehouseId,
						inWarehouseId:$scope.inWarehouseId,
						allocationCost : $scope.allocationCost,
						hopeOutTime : $scope.hopeOutTime,
						hopeInTime : $scope.hopeInTime,
						remark : $scope.remark,
						details : $scope.allocationorderList
				}
				if(!$scope.outWarehouseId){
					Kalert("请选择调出仓库");
					return;
				}
				if(!$scope.inWarehouseId){
					Kalert("请选择调入仓库");
					return;
				}
				if($scope.outWarehouseId ==$scope.inWarehouseId){
					Kalert("不能向与调出仓库相同的仓库调拨，请修改");
					return;
				}
				var msg = 0;
				if ($scope.allocationorderList.length > 0) {
					var numtrue = true;
					angular.forEach($scope.allocationorderList, function(alo) {
						if (alo.outGoodsId) {
							msg = msg + 1;
							if(!alo.planQuantity || alo.planQuantity*1<1){
								Kalert("商品:["+alo.outGoodsName+"]请输入正确的调拨数量");
								numtrue = false;
							}
							if(alo.outGoodsUsestock*1<alo.planQuantity*1){
								Kalert("商品:["+alo.outGoodsName+"]调拨数量不能大于可用数量");
								numtrue = false;
							}
							if(!alo.inGoodsId){
								Kalert("商品:["+alo.outGoodsName+"]请输入对应的调入商品货号");
								numtrue = false;
							}
						}
					});
					if(!numtrue)return;
				}
				if (msg < 1) {
					Kalert("请添加商品明细");
					return;
				}
				allocationService.saveData(submitData).then(
						function(data) {
							Kalert("保存成功",null,function(){
								$scope.back();
							});
						}, function(data) {
							Kalert(data.msg);
						});
			}
			//更换调拨仓库
			$scope.whSelectChange = function() {
				if ($scope.outWarehouseId === outWasrehouseIdTemp && $scope.inWarehouseId === inWasrehouseIdTemp) {
					return false;
				}
				var msg = 0;
				if ($scope.allocationorderList.length > 0) {
					angular.forEach($scope.allocationorderList, function(alo) {
						if (alo.outGoodsId||alo.inGoodsId) {
							msg = msg + 1;
						}
					});
				}
				if (msg > 0) {
					Kconfirm('更换调拨仓库将清空商品信息的所有数据，是否继续？', '更换仓库', function(r) {
						if (r) {
							outWasrehouseIdTemp = $scope.outWarehouseId;
							inWasrehouseIdTemp = $scope.inWarehouseId;
							$scope.allocationorderList = [];
							addline();
						} else {
							$scope.outWarehouseId = outWasrehouseIdTemp;
							$scope.inWarehouseId = inWasrehouseIdTemp;
						}
						$scope.$apply();
					});
				} else {
					outWasrehouseIdTemp = $scope.outWarehouseId;
					inWasrehouseIdTemp = $scope.inWarehouseId;
				}
			}
			
			//清空数据
			var clearData = function(index) {
				$scope.allocationorderList[index].outGoodsId ="";
				$scope.allocationorderList[index].outGoodsSku = "";
				$scope.allocationorderList[index].outGoodsBarcode = "";
				$scope.allocationorderList[index].outGoodsName = "";
				$scope.allocationorderList[index].outGoodsStock = "";
				$scope.allocationorderList[index].outGoodsPacking = "";
				$scope.allocationorderList[index].outGoodsUsestock = "";
				$scope.allocationorderList[index].planQuantity = "";
			}
			var clearDataIn = function(index) {
				$scope.allocationorderList[index].inGoodsId ="";
				$scope.allocationorderList[index].inGoodsSku = "";
				$scope.allocationorderList[index].inGoodsBarcode = "";
				$scope.allocationorderList[index].inGoodsName = "";
			}
			$scope.changeNum = function(index){
	       		var type = "^[0-9]*[1-9][0-9]*$";
	       		var re = new RegExp(type);
	       		if (isNaN($scope.allocationorderList[index].planQuantity)) {
	       			$scope.allocationorderList[index].planQuantity = "";
	       			return false;
	       		}
	       		if ($scope.allocationorderList[index].planQuantity.indexOf(".") >= 0) {
	       			$scope.allocationorderList[index].planQuantity = $scope.allocationorderList[index].planQuantity
	       					.replace(".", "");
	       		}
	       	}
			function IsExist(goodsId) {
				var num = 0;
				angular.forEach($scope.allocationorderList, function(los) {
					if (los.outGoodsId == goodsId) {
						num = num + 1;
					}
				});
				if (num >= 1) {
					return true;
				} else {
					return false;
				}
			}
			$scope.back = function() {
				window.location.href = "/allocation/index";
			}
});

function GetQueryString(sProp) {
	var re = new RegExp("[&,?]" + sProp + "=([^//&]*)", "i");
	var a = re.exec(window.location.search);
	if (a == null) {
		return "";
	} else {
		return decodeURIComponent(a[1]);
	}
}
function getLocalTime(nS) {     
    return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)
} 